package com.summer.thread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

public class SemaphoreTest {

	public static void main(String[] args) {
		
		ItemTask task1 = new ItemTask();
		ItemTask task2 = new ItemTask();
		ItemTask task3 = new ItemTask();
		ItemTask task4 = new ItemTask();
		ItemTask task5 = new ItemTask();
		ItemTask task6 = new ItemTask();
		ItemTask task7 = new ItemTask();
		ItemTask task8 = new ItemTask();
		ItemTask task9 = new ItemTask();
		ItemTask task10 = new ItemTask();
		
		task1.start();
		task2.start();
		task3.start();
		task4.start();
		task5.start();
		task6.start();
		task7.start();
		task8.start();
		task9.start();
		task10.start();
		
	}

	public static class ItemTask extends Thread{
		private static final Semaphore semaphore = new Semaphore(5);
		private static AtomicInteger number = new AtomicInteger(0);
		@Override
		public void run() {
			try {
				System.out.println("准备执行："+number.getAndIncrement());
				semaphore.acquire();
				System.out.println("获得执行中...");
				sleep(5000);
				System.out.println("执行完毕.");
			}catch (Exception e) {
				
			} finally {
				semaphore.release();
			}
		}
		
		
		
	}
	
}
